package com.chainyoung.trademarket.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chainyoung.common.imageloader.glide.transformations.RoundedCornersTransformationNew;
import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.ScreenUtils;
import com.chainyoung.common.view.banner.BannerEntry;
import com.chainyoung.component_tmmarket.bean.TitleImageBannerEntry;
import com.chainyoung.trademarket.R;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

public class ImageBannerEntry implements BannerEntry<String> {

    private final String url;
    private String title;
    private String subTitle;
    private String img;
    private Context mContext;
    private int width;

    public ImageBannerEntry(Context context, String title, String subTitle, String imgRes, String url) {
        this.title = title;
        this.subTitle = subTitle;
        this.img = imgRes;
        this.url = url;
        this.mContext = context;
        width = ScreenUtils.getScreenWidth(mContext);
    }

    @Override
    @NonNull
    public View onCreateView(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_index_banner_item, parent, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
//        int w = width - ConvertUtils.dp2px(mContext, 30);
//        params.width = w;
//        params.height = w / 2;
//        imageView.setLayoutParams(params);

        Glide.with(mContext)
                .load(img)
                .apply(bitmapTransform(new RoundedCornersTransformationNew(15, 0)))
                .into(imageView);
        return view;
    }

    /**
     * 获取标题
     *
     * @return 返回当前条目的标题。
     */
    @Override
    public CharSequence getTitle() {
        return title;
    }

    /**
     * 获取子标题。
     *
     * @return 返回当前条目的子标题。
     */
    @Nullable
    @Override
    public CharSequence getSubTitle() {
        return subTitle;
    }

    /**
     * 获取当前页面的数据。
     *
     * @return 返回当前页面的数据。
     */
    @Override
    public String getValue() {
        return url;
    }

    @Override
    public boolean same(BannerEntry newEntry) {
        return newEntry instanceof ImageBannerEntry && TextUtils.equals(title, newEntry.getTitle()) && TextUtils.equals(subTitle, newEntry.getSubTitle()) && img == ((ImageBannerEntry) newEntry).img;
    }
}

